package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.responseresult.OrderDetailData;
import com.chuanyang.driverh.utils.StringUtil;

import java.util.ArrayList;


public class GoodsAssignAdapter extends BaseListAdapter<OrderDetailData.DetailListBean> {//首页竞价大厅的adapter
    private Activity activity;
    private ArrayList<Boolean> imgs=new ArrayList<Boolean>();//记录checkBox的选择情况。
    private ArrayList<String> assignWeight=new ArrayList<String>();//记录edittext的数据。
//    private ArrayList<String> assignNum=new ArrayList<String>();//记录edittext的数据。
    private ArrayList<ViewHolder> holders=new ArrayList<ViewHolder>();//记录viewhold
    private boolean choose;

    public ArrayList<Boolean> getImgs() {
        return imgs;
    }
    public ArrayList<String> getAssignWeight() {
        return assignWeight;
    }

    int editPosition=-1;

    public GoodsAssignAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity=activity;
    }

    public void clearChoice(boolean choose){
        this.choose=choose;
        imgs.clear();
        assignWeight.clear();
//        assignNum.clear();
        editPosition=-1;
        for (int i=0;i<getmObjects().size();i++){
            imgs.add(i,choose);
            if (!choose){
                assignWeight.add(i,"");

            }else{
                OrderDetailData.DetailListBean bean=   (OrderDetailData.DetailListBean)   (getmObjects().get(i));

                if (Double.parseDouble(bean.getSurplusWeight())>0) {
                    assignWeight.add(i, bean.getSurplusWeight());
                }else {
                    assignWeight.add(i,"0");
                }

               /* if (Double.parseDouble(bean.getSurplusNum())>0) {
                    assignNum.add(i, bean.getSurplusNum());
                }else {
                    assignNum.add(i,"0");
                }*/
            }

        }
        notifyDataSetChanged();
    }


    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        if (holders.size()<=position ){
            holders.add(position,new ViewHolder());
        }
        if (imgs.size()<=position ){
            imgs.add(position,choose);
        }
        if (assignWeight.size()<=position ){
            assignWeight.add(position,"");
        }
       /* if (assignNum.size()<=position ){
            assignNum.add(position,"");
        }*/
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_assign_goods, parent, false);
            holders.get(position).iv_assign = (ImageView)convertView.findViewById(R.id.iv_assign);
            holders.get(position).tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            holders.get(position).tv_goods_stand = (TextView) convertView.findViewById(R.id.tv_goods_stand);
            holders.get(position).et_assign_weight = (EditText) convertView.findViewById(R.id.et_assign_weight);
            holders.get(position).tv_all_weight = (TextView) convertView.findViewById(R.id.tv_all_weight);
            holders.get(position).tv_weight_left = (TextView) convertView.findViewById(R.id.tv_weight_left);
//            holders.get(position).tv_num = (TextView) convertView.findViewById(R.id.tv_num);
//            holders.get(position).et_num = (EditText) convertView.findViewById(R.id.et_num);
            convertView.setTag(holders.get(position));
        } else {
            holders.remove(position);
            holders.add(position,(ViewHolder) convertView.getTag());
        }
       final OrderDetailData.DetailListBean  data= getItem(position);

        holders.get(position).tv_goods_name.setText(data.getGoodsName());
        holders.get(position).tv_goods_stand.setText("规格: "+data.getGoodsStandard());
//        holders.get(position).tv_all_weight.setText("总重量： "+data.getGoodsWeight()+"t");
//        holders.get(position).tv_weight_left.setText("未分配： "+data.getSurplusWeight()+"t");

//        holders.get(position).tv_all_weight.setText("货物总重量： ");
        if (Double.parseDouble(data.getSurplusWeight())<=0){
            holders.get(position).tv_weight_left.setText("0t");
        }else {
            holders.get(position).tv_weight_left.setText(StringUtil.getDouble(data.getSurplusWeight()+"")+"t");
        }

        /*if (Double.parseDouble(data.getSurplusNum())<=0){
            holders.get(position).tv_num.setText("0件");
        }else {
            holders.get(position).tv_num.setText(data.getSurplusNum()+"件");
        }*/

        holders.get(position).iv_assign.setSelected(imgs.get(position));
        setWeight( holders.get(position).et_assign_weight,position);


        holders.get(position).et_assign_weight.clearFocus();
        if (position==editPosition ){
            holders.get(position).et_assign_weight.requestFocus();
        }

        holders.get(position).iv_assign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holders.get(position).iv_assign.setSelected(!imgs.get(position));
                imgs.set(position,!imgs.get(position));
                if (imgs.get(position)){
                    if (Double.parseDouble(data.getSurplusWeight())>0){
                        assignWeight.set(position,data.getSurplusWeight());
                    }else {
                        assignWeight.set(position,"0");
                    }


                }else {
                    assignWeight.set(position,"");
                }


                setWeight( holders.get(position).et_assign_weight,position);
            }
        });

        holders.get(position).et_assign_weight.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                assignWeight.set(position, holders.get(position).et_assign_weight.getText().toString().trim());
                if (hasFocus) {
                    editPosition=position;
                }
            }
        });

        /*holders.get(position).et_num.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                assignNum.set(position, holders.get(position).et_num.getText().toString().trim());
            }
        });*/

        return convertView;
    }

    private  void setWeight(EditText tv_weight,int position){

        if (imgs.get(position)){

            tv_weight.setText(assignWeight.get(position));
            tv_weight.setSelection(tv_weight.getText().toString().length());
            tv_weight.setEnabled(true);
        }else{
            tv_weight.setText("");
            tv_weight.setEnabled(false);
        }
    }

    class ViewHolder {
        ImageView iv_assign;
        TextView tv_goods_name;
        TextView tv_goods_stand;
        EditText et_assign_weight;
        TextView tv_all_weight;
        TextView tv_weight_left;


//        TextView tv_num;
//        EditText et_num;
    }
}
